package com.example.romancalc;

import java.util.Arrays;

import org.apache.log4j.Logger;

import com.example.romancalc.core.IRomanCalc;
import com.example.romancalc.core.RomanCalcImpl;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

/**
 * @author Luis Oscar Trigueiros
 */
public class Calc extends VerticalLayout {

	// the 'POJO' we're editing
	Params params;

	IRomanCalc romanCalc = new RomanCalcImpl();
	/**
	 * 
	 */
	private static final long serialVersionUID = 7161229294287369352L;

	private static final Logger logger = Logger.getLogger(Calc.class);

	final Form calcForm = new Form();

	private Label resultLabel = new Label();

	private Button computeButton = new Button("Compute", new Button.ClickListener() {
		private static final long serialVersionUID = 8264450017198214341L;

		public void buttonClick(ClickEvent event) {
			try {
				resultLabel.setValue("");
				calcForm.commit();
				String op1 = params.getOp1();
				String op2 = params.getOp2();
				String result = romanCalc.add(op1, op2);
				params.setResult(result);
				logger.debug("result = " + params.getResult());
				displayResult();
			} catch (Exception e) {
				// Ignored, we'll let the Form handle the errors
				getWindow().showNotification("Invalid input:", e.getMessage(),
						Notification.TYPE_WARNING_MESSAGE);

				logger.error(String.format("Validation error %s", e.getMessage()));
			}
		}
	});
	private Button clearButton = new Button("Clear", new Button.ClickListener() {
		private static final long serialVersionUID = 8169965549539829036L;

		public void buttonClick(ClickEvent event) {
			params.clear();
			calcForm.setComponentError(null);
			calcForm.setValidationVisible(false);
		}
	});

	public Calc() {
		params = new Params();
		// item from POJO
		BeanItem<Params> paramsItem = new BeanItem<Params>(params);

		// Create the Form
		calcForm.setCaption("Roman Calculator");
		// we want explicit 'apply'
		calcForm.setWriteThrough(false);
		calcForm.setInvalidCommitted(false);

		// FieldFactory for customizing the fields and adding validators
		calcForm.setFormFieldFactory(new ParamsFieldFactory());

		// bind to POJO via BeanItem
		calcForm.setItemDataSource(paramsItem);

		// Determines which properties are shown, and in which order:
		calcForm.setVisibleItemProperties(Arrays.asList(new String[] { "op1", "op2" }));

		// Add form to layout
		addComponent(calcForm);
		Embedded hrImg = new Embedded("", new ThemeResource("../calcTheme/img/hbar.png"));
		calcForm.getLayout().addComponent(hrImg);
		calcForm.getLayout().addComponent(resultLabel);
		// The cancel / apply buttons
        HorizontalLayout buttons = new HorizontalLayout();
        buttons.setSpacing(true);
        buttons.addComponent(computeButton);
        //buttons.addComponent(clearButton);
		calcForm.getFooter().addComponent(buttons);
		calcForm.getFooter().setMargin(false, false, true, true);
	}

	private void displayResult() {
		resultLabel.setValue(params.getResult());
		Window.Notification n = new Window.Notification("Result",
				Window.Notification.TYPE_TRAY_NOTIFICATION);
		n.setPosition(Window.Notification.POSITION_CENTERED);
		n.setDescription(params.getResult());
		getWindow().showNotification(n);
	}

}
